package a10_动态规划;

import java.util.Arrays;
import java.util.Scanner;

/**
 * <p>
 * a12_背包理论基础2复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/3/12
 */
public class a12_背包理论基础2复习3 {
    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//
//        int num = scanner.nextInt();
//        int maxWeight = scanner.nextInt();
//
//        int[] weight = new int[num];
//        int[] value = new int[num];
//
//        for (int i = 0; i < weight.length; i++) {
//            weight[i] = scanner.nextInt();
//        }
//
//        for (int i = 0; i < value.length; i++) {
//            value[i] = scanner.nextInt();
//        }

        int num = 6;
        int maxWeight = 1;

        int[] weight = new int[]{1,2,3,1,5,2};
        int[] value = new int[]{2,3,1,5,4,3};

        //dp[i][j]：0~i的物品装满容量为j的背包的最大价值

        //不装i：dp[i-1][j]
        //装i：dp[i-1][j-weight[i]] + value[i]

        //使用一个数组存放：
        //不装：dp[j]
        //装：dp[j-weight[i]] + value[i]

        //注意：如果遍历容量的时候顺时针会覆盖
        //因为dp[j] = Math.max(dp[j],dp[j-weight[i]] + value[i]);依赖于dp[j-weight[i]]

        //初始化：
        //if(weight[0] < j){
        //   dp[0][j] = value[0];
        //}

        int[] dp = new int[maxWeight+1];
        for (int i = 0; i < weight.length; i++) {
            for (int j = maxWeight; j >= weight[i]; j--) {
                dp[j] = Math.max(dp[j],dp[j-weight[i]] + value[i]);
            }
            System.out.println(Arrays.toString(dp));
        }

        System.out.println(dp[maxWeight]);
    }
}
